﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class Props : MgConfig
    {
        private Dictionary<int, Jy123.Model.Public_JType> JTypeDic = new Dictionary<int, Jy123.Model.Public_JType>();
        private Dictionary<int, Jy123.Model.Public_PropsType> propsTypeDic = new Dictionary<int, Jy123.Model.Public_PropsType>();
        private Dictionary<int, Jy123.Model.Public_Level> levelDic = new Dictionary<int, Jy123.Model.Public_Level>();
        private List<Jy123.Model.Public_JType> listJType;

        private List<Jy123.Model.Public_PropsType> grandPropsType;
        private List<Jy123.Model.Public_PropsType> allPropsType;
        private List<Jy123.Model.Public_Level> listLevel;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Props.aspx");
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("JFather IS NOT NULL");
            listJType = BLLJType.GetModelList(strWhere.ToString());
            foreach (Jy123.Model.Public_JType JType in listJType)
            {
                JTypeDic.Add(JType.ID, JType);
            }
            Jy123.BLL.Public_PropsType BLLPropsType = new Jy123.BLL.Public_PropsType();
            allPropsType = BLLPropsType.GetModelList(string.Empty);
            grandPropsType = BLLPropsType.GetModelList("PFather IS NULL");
            foreach (Jy123.Model.Public_PropsType propsType in allPropsType)
            {
                propsTypeDic.Add(propsType.ID, propsType);
            }
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            listLevel = BLLLevel.GetModelList(string.Empty);
            foreach (Jy123.Model.Public_Level level in listLevel)
            {
                levelDic.Add(level.ID, level);
            }
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Props props = e.Item.DataItem as Jy123.Model.Public_Props;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = props.ID.ToString();

                if (props.FJType.HasValue)
                {
                    Label JTypeLabel = e.Item.FindControl("JTypeLabel") as Label;
                    JTypeLabel.Text = JTypeDic[props.FJType.Value].JName;
                }
                if (props.FPType.HasValue)
                {
                    Label propsTypeLabel = e.Item.FindControl("propsTypeLabel") as Label;
                    propsTypeLabel.Text = propsTypeDic[props.FPType.Value].PName;
                }
                Label nameLabel = e.Item.FindControl("nameLabel") as Label;
                nameLabel.Text = props.PName;
                if (props.PhysicalFitness.HasValue)
                {
                    Label fittnessLabel = e.Item.FindControl("fittnessLabel") as Label;
                    fittnessLabel.Text = props.PhysicalFitness.Value.ToString();
                }
                if (props.Liveness.HasValue)
                {
                    Label livenessLabel = e.Item.FindControl("livenessLabel") as Label;
                    livenessLabel.Text = props.Liveness.Value.ToString();
                }
                if (props.Skill.HasValue)
                {
                    Label skillLabel = e.Item.FindControl("skillLabel") as Label;
                    skillLabel.Text = props.Skill.Value.ToString();
                }
                if (props.Moral.HasValue)
                {
                    Label moralLabel = e.Item.FindControl("moralLabel") as Label;
                    moralLabel.Text = props.Moral.Value.ToString();
                }

                Label usingLevelLabel = e.Item.FindControl("usingLevelLabel") as Label;
                string strUL = props.UsingLevel;
                if (!string.IsNullOrEmpty(strUL))
                {
                    string strResult = string.Empty;
                    string[] strArray = strUL.Split(',');
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(strArray[i]))
                        {
                            int id = int.Parse(strArray[i].Replace(",", ""));
                            strResult += levelDic[id].LName + ",";
                        }
                    }
                    if (strResult.Length > 1)
                    {
                        usingLevelLabel.Text = strResult.Remove(strResult.LastIndexOf(","));
                    }

                }
                if (props.Quantity.HasValue)
                {
                    Label quantityLabel = e.Item.FindControl("quantityLabel") as Label;
                    quantityLabel.Text = props.Quantity.Value.ToString();
                }
                if (props.Price.HasValue)
                {
                    Label priceLabel = e.Item.FindControl("priceLabel") as Label;
                    priceLabel.Text = (Convert.ToInt32(props.Price.Value)).ToString();
                }
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Props BLLProps = new Jy123.BLL.Public_Props();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Props props = BLLProps.GetModel(id);
                if (props.FJType.HasValue)
                {
                    JTypeDropDown.SelectedValue = props.FJType.Value.ToString();
                    JTypeDropDown_Change(JTypeDropDown, EventArgs.Empty);
                }
                if (props.FPType.HasValue)
                {
                    Model.Public_PropsType type = propsTypeDic[props.FPType.Value];
                    if (type.PFather.HasValue)
                    {
                        Model.Public_PropsType fatherType = propsTypeDic[type.PFather.Value];
                        if (fatherType.PFather.HasValue)
                        {
                            Model.Public_PropsType grandType = propsTypeDic[fatherType.PFather.Value];
                            grandDropDown.SelectedValue = grandType.ID.ToString();
                            Grand_Change(grandDropDown, EventArgs.Empty);
                            fatherDropDown.SelectedValue = fatherType.ID.ToString();
                            Father_Change(fatherDropDown, EventArgs.Empty);
                            propsTypeDropDown.SelectedValue = type.ID.ToString();
                        }
                        else
                        {
                            grandDropDown.SelectedValue = fatherType.ID.ToString();
                            Grand_Change(grandDropDown, EventArgs.Empty);
                            fatherDropDown.SelectedValue = type.ID.ToString();
                            Father_Change(fatherDropDown, EventArgs.Empty);
                        }
                    }
                    else
                    {
                        grandDropDown.SelectedValue = type.ID.ToString();
                        Grand_Change(grandDropDown, EventArgs.Empty);
                    }                    
                }

                nameTextBox.Text = props.PName;
                if (props.PhysicalFitness.HasValue)
                {
                    fittnessTextBox.Text = props.PhysicalFitness.Value.ToString();
                }
                if (props.Liveness.HasValue)
                {
                    livenessTextBox.Text = props.Liveness.Value.ToString();
                }
                if (props.Skill.HasValue)
                {
                    skillTextBox.Text = props.Skill.Value.ToString();
                }
                if (props.Moral.HasValue)
                {
                    moralTextBox.Text = props.Moral.Value.ToString();
                }

                if (props.Quantity.HasValue)
                {
                    quantityTextBox.Text = props.Quantity.Value.ToString();
                }
                if (props.Price.HasValue)
                {
                    priceTextBox.Text = (Convert.ToInt32(props.Price.Value)).ToString();
                }

                string[] strLevel = props.UsingLevel.Split(',');
                foreach (ListItem clItem in ulCheckBoxList.Items)
                {
                    for (int i = 0; i < strLevel.Length; i++)
                    {
                        if (strLevel[i].Equals(clItem.Value))
                        {
                            clItem.Selected = true;
                        }
                    }
                }
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                propsSmallImage.Src = props.SmallModel;
                propsBigImage.Src = props.Model;
                if (displayDiv.Visible)
                {
                    displayOrderTextBox.Text = props.DisplayOrder.ToString();
                    posXTextBox.Text = props.PosX.ToString();
                    posYTextBox.Text = props.PosY.ToString();
                }
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLProps.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void JTypeDropDown_Change(object sender, EventArgs e)
        {
            int id = int.Parse(JTypeDropDown.SelectedValue);
            BindCheckBoxList(id);
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Props BLLProps = new Jy123.BLL.Public_Props();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Props props = new Jy123.Model.Public_Props();
                SetValue(props);
                int typeId = BLLProps.Add(props);
                props.Model = CreateLogo(typeId, fileUploadBig.PostedFile, "Model");
                props.SmallModel = CreateLogo(typeId, fileUploadSmall.PostedFile, "SmallModel");
                props.ID = typeId;
                BLLProps.Update(props);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Props props = BLLProps.GetModel(id);
                SetValue(props);
                if (fileUploadBig.PostedFile != null && fileUploadSmall.PostedFile != null)
                {
                    string strLogo = CreateLogo(id, fileUploadBig.PostedFile, "Model");
                    string strLogoSmall = CreateLogo(id, fileUploadSmall.PostedFile, "SmallModel");
                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        props.Model = strLogo;
                    }
                }
                BLLProps.Update(props);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void SetValue(Jy123.Model.Public_Props props)
        {
            props.PName = nameTextBox.Text;
            if (int.Parse(JTypeDropDown.SelectedValue) > 0)
            {
                props.FJType = int.Parse(JTypeDropDown.SelectedValue);
            }

            props.PName = nameTextBox.Text;
            if (!string.IsNullOrEmpty(fittnessTextBox.Text))
            {
                props.PhysicalFitness = int.Parse(fittnessTextBox.Text);
            }
            if (!string.IsNullOrEmpty(livenessTextBox.Text))
            {
                props.Liveness = int.Parse(livenessTextBox.Text);
            }
            if (!string.IsNullOrEmpty(skillTextBox.Text))
            {
                props.Skill = int.Parse(skillTextBox.Text);
            }
            if (!string.IsNullOrEmpty(moralTextBox.Text))
            {
                props.Moral = int.Parse(moralTextBox.Text);
            }
            if (!string.IsNullOrEmpty(quantityTextBox.Text))
            {
                props.Quantity = int.Parse(quantityTextBox.Text);
            }
            if (!string.IsNullOrEmpty(priceTextBox.Text))
            {
                props.Price = decimal.Parse(priceTextBox.Text);
            }
            string strLevel = ",";
            foreach (ListItem item in ulCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    strLevel += item.Value + ",";
                }
            }
            if (strLevel.Length > 1)
            {
                props.UsingLevel = strLevel;
            }
            if (typeLi.Visible)
            {
                if (int.Parse(propsTypeDropDown.SelectedValue) > 0)
                {
                    props.FPType = int.Parse(propsTypeDropDown.SelectedValue);
                }
            }
            else
            {
                if (fatherLi.Visible)
                {
                    if (int.Parse(fatherDropDown.SelectedValue) > 0)
                    {
                        props.FPType = int.Parse(fatherDropDown.SelectedValue);
                    }
                }
                else
                {
                    if (int.Parse(grandDropDown.SelectedValue) > 0)
                    {
                        props.FPType = int.Parse(grandDropDown.SelectedValue);
                    }
                }
            }
            if (displayDiv.Visible)
            {
                if (!string.IsNullOrEmpty(displayOrderTextBox.Text))
                {
                    props.DisplayOrder = int.Parse(displayOrderTextBox.Text);
                }
                if (!string.IsNullOrEmpty(posXTextBox.Text))
                {
                    props.PosX = int.Parse(posXTextBox.Text);
                }
                if (!string.IsNullOrEmpty(posYTextBox.Text))
                {
                    props.PosY = int.Parse(posYTextBox.Text);
                }
            }
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            nameTextBox.Text = string.Empty;
            fittnessTextBox.Text = string.Empty;
            skillTextBox.Text = string.Empty;
            moralTextBox.Text = string.Empty;
            quantityTextBox.Text = string.Empty;
            priceTextBox.Text = string.Empty;
            livenessTextBox.Text = string.Empty;
            displayDiv.Visible = false;
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_Props BLLProps = new Jy123.BLL.Public_Props();
            List<Jy123.Model.Public_Props> listProps = BLLProps.GetModelList(string.Empty);
            propsRepeater.DataSource = listProps;
            propsRepeater.DataBind();
            propsRepeater.Visible = listProps.Count > 0;
            BindDropDown();
        }

        private void BindDropDown()
        {
            JTypeDropDown.Items.Clear();
            JTypeDropDown.DataSource = listJType;
            JTypeDropDown.DataTextField = "JName";
            JTypeDropDown.DataValueField = "ID";
            JTypeDropDown.DataBind();
            JTypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));

            grandDropDown.Items.Clear();
            grandDropDown.DataSource = grandPropsType;
            grandDropDown.DataTextField = "PName";
            grandDropDown.DataValueField = "ID";
            grandDropDown.DataBind();
            grandDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
            fatherLi.Visible = false;
            typeLi.Visible = false;
        }

        private void BindDropDown(DropDownList dropDown, List<Model.Public_PropsType> listType)
        {
            dropDown.Items.Clear();
            dropDown.DataSource = listType;
            dropDown.DataTextField = "PName";
            dropDown.DataValueField = "ID";
            dropDown.DataBind();
            dropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        protected void Grand_Change(object sender, EventArgs e)
        {
            typeLi.Visible = false;
            int id = int.Parse(grandDropDown.SelectedValue);
            BLL.Public_PropsType BLLType = new Jy123.BLL.Public_PropsType();
            List<Model.Public_PropsType> listPropsType = BLLType.GetModelList("PFather = " + id.ToString());
            BindDropDown(fatherDropDown, listPropsType);
            fatherLi.Visible = listPropsType.Count > 0;
            string selectText = grandDropDown.SelectedItem.ToString();
            displayDiv.Visible = selectText.Equals("装备");
        }

        protected void Father_Change(object sender, EventArgs e)
        {
            int id = int.Parse(fatherDropDown.SelectedValue);
            BLL.Public_PropsType BLLType = new Jy123.BLL.Public_PropsType();
            List<Model.Public_PropsType> listPropsType = BLLType.GetModelList("PFather = " + id.ToString());
            BindDropDown(propsTypeDropDown, listPropsType);
            typeLi.Visible = listPropsType.Count > 0;
        }

        private void BindCheckBoxList(int id)
        {
            ulCheckBoxList.Items.Clear();
            ulDiv.Visible = id > 0;
            if (id > 0)
            {
                string strWhere = string.Format("FJType = {0} ORDER BY LevelNumber", id);
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                List<Jy123.Model.Public_Level> listLevel = BLLLevel.GetModelList(strWhere);
                ulCheckBoxList.DataSource = listLevel;
                ulCheckBoxList.DataTextField = "LName";
                ulCheckBoxList.DataValueField = "ID";
                ulCheckBoxList.DataBind();
            }
        }

        protected string CreateLogo(int propsId, HttpPostedFile postFile, string modelType)
        {
            CreateDir createDir = new CreateDir();
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreatePropsPath(CreateDir.PROPS, propsId, logoType, modelType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
